

#include "etcd.hpp"
#include "channel.hpp"
#include <gflags/gflags.h>
#include <thread>
#include <cctype>

#include "base.pb.h"
#include "user.pb.h"
#include "transmite.pb.h"
#include "utils.hpp"

#include <fstream>
#include <stdexcept>


DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "服务监控根目录");

xu::ServiceManage::ptr sm;

void string_message(const std::string &uid,const std::string &ssid, const std::string & content)
{
    auto channel = sm->Choose(FLAGS_transmite_service);
    if (!channel) 
    {
        LOG_ERROR("获取信道出错");
        return;    
    }

    xu::MsgTransmitService_Stub stub(channel.get());
    xu::NewMessageReq request;
    xu::GetTransmitTargetRsp response;
    request.set_request_id(xu::uuid());
    request.set_user_id(uid);
    request.set_chat_session_id(ssid);
    request.mutable_message()->set_message_type(xu::MessageType::STRING);
    request.mutable_message()->mutable_string_message()->set_content(content);
    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &request, &response, nullptr);
    
    if(cntl.Failed() == true || response.success() == false)
    {
        std::cout<<"失败"<<std::endl;
    }

    for(int i = 0; i < response.target_id_list_size(); ++i)
    {
        std::cout<<response.target_id_list(i)<<std::endl;
    }
}

void image_message(const std::string &uid, const std::string &ssid, const std::string &msg) 
{
    auto channel = sm->Choose(FLAGS_transmite_service);
    if (!channel) 
    {
        LOG_ERROR("获取信道出错");
        return;    
    }

    xu::MsgTransmitService_Stub stub(channel.get());
    xu::NewMessageReq request;
    xu::GetTransmitTargetRsp response;
    request.set_request_id(xu::uuid());
    request.set_user_id(uid);
    request.set_chat_session_id(ssid);
    request.mutable_message()->set_message_type(xu::MessageType::IMAGE);
    request.mutable_message()->mutable_image_message()->set_image_content(msg);

    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &request, &response, nullptr);
    
    if(cntl.Failed() == true || response.success() == false)
    {
        std::cout<<"失败"<<std::endl;
    }
}

void speech_message(const std::string &uid, const std::string &ssid, const std::string &msg) 
{
    auto channel = sm->Choose(FLAGS_transmite_service);
    if (!channel) 
    {
        LOG_ERROR("获取信道出错");
        return;    
    }

    xu::MsgTransmitService_Stub stub(channel.get());
    xu::NewMessageReq request;
    xu::GetTransmitTargetRsp response;
    request.set_request_id(xu::uuid());
    request.set_user_id(uid);
    request.set_chat_session_id(ssid);
    request.mutable_message()->set_message_type(xu::MessageType::SPEECH);
    request.mutable_message()->mutable_speech_message()->set_file_contents(msg);

    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &request, &response, nullptr);
    
    if(cntl.Failed() == true || response.success() == false)
    {
        std::cout<<"失败"<<std::endl;
    }
}

void file_message(const std::string &uid, const std::string &ssid, 
    const std::string &filename, const std::string &content) 
{
     auto channel = sm->Choose(FLAGS_transmite_service);
    if (!channel) 
    {
        LOG_ERROR("获取信道出错");
        return;    
    }

    xu::MsgTransmitService_Stub stub(channel.get());
    xu::NewMessageReq request;
    xu::GetTransmitTargetRsp response;
    request.set_request_id(xu::uuid());
    request.set_user_id(uid);
    request.set_chat_session_id(ssid);
    request.mutable_message()->set_message_type(xu::MessageType::FILE);
    request.mutable_message()->mutable_file_message()->set_file_contents(content);
    request.mutable_message()->mutable_file_message()->set_file_name(filename);
    request.mutable_message()->mutable_file_message()->set_file_size(content.size());

    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &request, &response, nullptr);
    
    if(cntl.Failed() == true || response.success() == false)
    {
        std::cout<<"失败"<<std::endl;
    }
}


int main(int argc, char *argv[])
{   
    google::ParseCommandLineFlags(&argc, &argv, true);
    InitLog(FLAGS_run_mode,FLAGS_log_file,FLAGS_log_level);

    //1. 先构造Rpc信道管理对象
    sm = std::make_shared<xu::ServiceManage>();
    sm->AddFollow(FLAGS_transmite_service);
    auto put_cb = std::bind(&xu::ServiceManage::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&xu::ServiceManage::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    //2. 构造服务发现对象
    xu::Discovery::ptr dclient = std::make_shared<xu::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);

    // string_message("648c-19b3c52f-0000","会话ID1","吃饭了吗？");
    // string_message("f05c-2f9cebd0-0001","会话ID1","吃的盖浇饭！！");
    // image_message("648c-19b3c52f-0000", "会话ID1", "可爱表情图片数据");
    //speech_message("648c-19b3c52f-0000", "1034-1ad96831-0000", "动听猪叫声数据");
    file_message("18b4-388d3e25-0001", "061a-f1cf3a4a-0002", "猪爸爸的文件名称", "猪爸爸的文件数据");
    file_message("e6e5-f9c15719-0002", "1034-1ad96831-0000", "猪妈妈的文件名称", "猪妈妈的文件数据");

    return 0;
}
